package com.mycompany.sigemp.web;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mycompany.sigemp.model.Maquinaria;
import com.mycompany.sigemp.service.MaquinariaService;

@Controller
public class MaquinariaController {
	@Autowired
	MaquinariaService mService;
	
	@RequestMapping(value="/listar.gsp")
    public String listarMaquinarias(Model model, HttpSession session){
		List<Maquinaria> lista = mService.ObtenerTodasLasMaquinarias();
		model.addAttribute("data", lista);
		model.addAttribute("user", session.getAttribute("user"));
		return "viewListarMaquinaria";	
	}
	
	@RequestMapping(value="/inicio.gsp")
	public String vistaInicio(){
		return "home";
	}
	
	@RequestMapping(value="/buscarxestado.gsp")
	public String vistaBuscarMaquinariasEstado(Model model, HttpSession session){
		model.addAttribute("user", session.getAttribute("user"));
		return "viewBuscarMaquinariaEstado";
	}
	
	@RequestMapping(value="/buscarPorEstado.gsp",method=RequestMethod.POST)
	public String procesarBusquedaEstado(@RequestParam String selectEstado, Model model, HttpSession session){
		if(selectEstado.equals("Seleccione")){
			model.addAttribute("user", session.getAttribute("user"));
			return "viewBuscarMaquinariaEstado";
		}
		List<Maquinaria> lista = mService.obtenerTodasLasMaquinariasPorEstado(selectEstado); 
		model.addAttribute( "data" , lista);
		return "viewListarMaquinaria";
	}
	
	@RequestMapping(value="/buscarxmodelo.gsp")
	public String vistaBuscarMaquinariasModelo(Model model, HttpSession session){
		model.addAttribute("user", session.getAttribute("user"));
		return "viewBuscarMaquinariaModelo";
	}
	
	@RequestMapping(value="/buscarPorModelo.gsp",method=RequestMethod.POST)
	public String procesarBusquedaModelo(@RequestParam String txtModelo, Model model, HttpSession session){
		if(txtModelo.equals("")){
			model.addAttribute("user", session.getAttribute("user"));
			return "viewBuscarMaquinariaModelo";
		}
		List<Maquinaria> lista = mService.obtenerMaquinariaPorPlaca(txtModelo); 
		model.addAttribute( "data" , lista);
		return "viewListarMaquinaria";
	}
	
	@RequestMapping(value="/registrar.gsp")
	public String vistaRegistrar(Model model, HttpSession session){
		model.addAttribute(new Maquinaria());
		model.addAttribute("user", session.getAttribute("user"));
		return "viewRegistrarMaquinaria";  
	}
	
	@RequestMapping(value="/registrar.gsp",method=RequestMethod.POST)
	public String procesarRegistroDeMaquinaria(@Valid Maquinaria maquinaria, BindingResult result, Model model, HttpSession session){
		if (result.hasErrors()) {
			model.addAttribute("user", session.getAttribute("user"));
			return "viewRegistrarMaquinaria";
		}
		mService.registrarUnaMaquinaria(maquinaria);
		model.addAttribute(new Maquinaria());
		model.addAttribute("user", session.getAttribute("user"));
		return "viewRegistrarMaquinaria";
	}
	
	@RequestMapping(value = "/editar.gsp", method = RequestMethod.GET) 
    public String verParaEditar(@RequestParam String code, Model model, HttpSession session){
        Maquinaria m = mService.obtenerMaquinariaPorCodigo(code);
        model.addAttribute("user", session.getAttribute("user"));
        model.addAttribute( "maquinaria" , m);     
        return "viewActualizarMaquinaria";
    }
	
	@RequestMapping(value="/actualizar.gsp",method=RequestMethod.POST)
	public String actualizarMaquinaria(@Valid Maquinaria maquinaria, BindingResult result, Model model, HttpSession session){
		if (result.hasErrors()) {
			model.addAttribute("user", session.getAttribute("user"));
			return "viewActualizarMaquinaria";
		}
		mService.actualizarMaquinaria(maquinaria);
		List<Maquinaria> lista = mService.ObtenerTodasLasMaquinarias();
		model.addAttribute("data", lista);
		return "viewListarMaquinaria";	
	}
	
	@RequestMapping(value = "/eliminar.gsp", method = RequestMethod.GET) 
    public String verParaEliminar(@RequestParam String code, Model model, HttpSession session){
        Maquinaria m = mService.obtenerMaquinariaPorCodigo(code);
        model.addAttribute("user", session.getAttribute("user"));
        model.addAttribute( "maquinaria" , m);     
        return "viewEliminarMaquinaria";
    }
	
	@RequestMapping(value="/delete.htm",method=RequestMethod.POST)
	public String eliminarMaquinaria(@Valid Maquinaria maquinaria, BindingResult result, Model model){
		if (result.hasErrors()) {
			return "viewEliminarMaquinaria";
		}
		mService.eliminarUnaMaquinaria(maquinaria);
		List<Maquinaria> lista = mService.ObtenerTodasLasMaquinarias();
		model.addAttribute("data", lista);
		return "viewListarMaquinaria";	
	}
	
	@RequestMapping(value = "/detalle.gsp", method = RequestMethod.GET) 
    public String detalleMaquinaria(@RequestParam String codigo, Model model, HttpSession session){
        Maquinaria m = mService.obtenerMaquinariaPorCodigo(codigo);
        model.addAttribute("user", session.getAttribute("user"));
        model.addAttribute( "maquinaria" , m);     
        return "viewDetalleMaquinaria";
    }
	
	
}
